﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class BidderCollateralDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public BidderCollateralDAL()
        {

        }

        public BidderCollateralInfo GetBidderCollateral(int ColSeq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            BidderCollateralInfo info = new BidderCollateralInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@icm_col_seq", ColSeq);

                strSQL = "SELECT bc.*"
                        + ", ct.ICM_COL_TYPE_NAME"
                    + " FROM ICM_BIDDER_COLLATERAL bc"
                        + " INNER JOIN ICM_SYS_COLLATERAL_TYPE ct ON bc.ICM_COL_TYPE_SEQ = ct.ICM_COL_TYPE_SEQ"
                    + " WHERE bc.ICM_BIDDER_COL_SEQ = @icm_col_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    SysCollateralTypeInfo infoType = new SysCollateralTypeInfo();
                    BidderInfo infoBidder = new BidderInfo();

                    info.Seq = Convert.ToInt32(dr[0]["ICM_BIDDER_COL_SEQ"]);
                    infoType.Seq = (Convert.IsDBNull(dr[0]["ICM_COL_TYPE_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[0]["ICM_COL_TYPE_SEQ"]);
                    infoType.Name = dr[0]["ICM_COL_TYPE_NAME"].ToString();
                    info.RefNo = dr[0]["ICM_BIDDER_COL_REF_NO"].ToString();
                    info.ReceiptNo = dr[0]["RECEIPT_NO"].ToString();
                    info.Amount = (Convert.IsDBNull(dr[0]["ICM_BIDDER_COL_AMOUNT"])) ? null : (Nullable<double>)Convert.ToInt32(dr[0]["ICM_BIDDER_COL_AMOUNT"]);
                    info.Descr = dr[0]["ICM_BIDDER_COL_DESCR"].ToString();
                    infoBidder.Seq = (Convert.IsDBNull(dr[0]["ICM_BIDDER_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[0]["ICM_BIDDER_SEQ"]);

                    info.InfoType = infoType;
                    info.InfoBidder = infoBidder;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public IList<BidderCollateralInfo> GetBidderCollateralFromParent(int BidderSeq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<BidderCollateralInfo> infoList = new List<BidderCollateralInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@icm_bidder_seq", BidderSeq);

                strSQL = "SELECT bc.*"
                        + ", ct.ICM_COL_TYPE_NAME"
                    + " FROM ICM_BIDDER_COLLATERAL bc"
                        + " INNER JOIN ICM_SYS_COLLATERAL_TYPE ct ON bc.ICM_COL_TYPE_SEQ = ct.ICM_COL_TYPE_SEQ"
                    + " WHERE bc.ICM_BIDDER_SEQ = @icm_bidder_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        BidderCollateralInfo info = new BidderCollateralInfo();
                        SysCollateralTypeInfo infoType = new SysCollateralTypeInfo();
                        BidderInfo infoBidder = new BidderInfo();

                        info.Seq = Convert.ToInt32(dr[i]["ICM_BIDDER_COL_SEQ"]);
                        infoType.Seq = (Convert.IsDBNull(dr[i]["ICM_COL_TYPE_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[i]["ICM_COL_TYPE_SEQ"]);
                        infoType.Name = dr[i]["ICM_COL_TYPE_NAME"].ToString();
                        info.RefNo = dr[i]["ICM_BIDDER_COL_REF_NO"].ToString();
                        info.ReceiptNo = dr[i]["RECEIPT_NO"].ToString();
                        info.Amount = (Convert.IsDBNull(dr[i]["ICM_BIDDER_COL_AMOUNT"])) ? null : (Nullable<double>)Convert.ToInt32(dr[i]["ICM_BIDDER_COL_AMOUNT"]);
                        info.Descr = dr[i]["ICM_BIDDER_COL_DESCR"].ToString();
                        infoBidder.Seq = (Convert.IsDBNull(dr[i]["ICM_BIDDER_SEQ"])) ? null : (Nullable<int>)Convert.ToInt32(dr[i]["ICM_BIDDER_SEQ"]);

                        info.InfoType = infoType;
                        info.InfoBidder = infoBidder;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public void Insert(BidderCollateralInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@ref_no", info.RefNo);
                dbManager.AddParameters(1, "@col_type_seq", info.InfoType.Seq);
                dbManager.AddParameters(2, "@bidder_seq", info.InfoBidder.Seq);
                dbManager.AddParameters(3, "@amount", info.Amount);
                dbManager.AddParameters(4, "@descr", info.Descr);
                dbManager.AddParameters(5, "@created_by", info.InfoRecord.Created_by);
                dbManager.AddParameters(6, "@created_when", info.InfoRecord.Created_when);

                strSQL = "INSERT INTO ICM_BIDDER_COLLATERAL"
                        + "(ICM_COL_TYPE_SEQ, ICM_BIDDER_COL_REF_NO, ICM_BIDDER_COL_AMOUNT"
                        + ", ICM_BIDDER_COL_DESCR, ICM_BIDDER_SEQ"
                        + ", CREATED_BY, CREATED_WHEN)"
                        + " VALUES(@col_type_seq"
                        + ", @ref_no"
                        + ", @amount"
                        + ", @descr"
                        + ", @bidder_seq"
                        + ", @created_by"
                        + ", @created_when"
                        + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(BidderCollateralInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@ref_no", info.RefNo);
                dbManager.AddParameters(2, "@col_type_seq", info.InfoType.Seq);
                dbManager.AddParameters(3, "@bidder_seq", info.InfoBidder.Seq);
                dbManager.AddParameters(4, "@amount", info.Amount);
                dbManager.AddParameters(5, "@descr", info.Descr);
                dbManager.AddParameters(6, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(7, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_BIDDER_COLLATERAL"
                    + " SET ICM_COL_TYPE_SEQ = @col_type_seq"
                        + ", ICM_BIDDER_COL_REF_NO = @ref_no"
                        + ", ICM_BIDDER_COL_AMOUNT = @amount"
                        + ", ICM_BIDDER_COL_DESCR = @descr"
                        + ", ICM_BIDDER_SEQ = @bidder_seq"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_BIDDER_COL_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteFromDB(IList<BidderCollateralInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();

                strSQL = "DELETE FROM ICM_BIDDER_COLLATERAL"
                    + " WHERE ICM_BIDDER_COL_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
